// // pages/serve/chapter/chapter.js
var mistakesArr = []//错题
var correct = 0
//触屏开始点坐标
var startDot = {
  X: 0,
  Y: 0
};
//触屏到点坐标
var touchDot = {
  X: 0,
  Y: 0
};
var time2 = 0;  //触屏时间
var number;     //定时器ID
var newIndex = 0//设置活动菜单的index
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */

  data: {
    bar_Height: wx.getSystemInfoSync().statusBarHeight + 45,
    // chapterName: "章节练习",
    title: "1",
    chapterClickTops: true,
    topicArr: [],
    gif: "true",

    topicObj: {},
    topicArrInd: 0,//正在做的题的数组下标
    topicYes: 0,//正确
    topicNo: 0,//错误
    view1: 'selection1',
    view2: 'selection1',
    view3: 'selection1',
    view4: 'selection1',
    falseOption: ['', '', '', ''],
    tureOption: ['', '', '', ''],
    options: ["A", "B", "C", "D"],
    videoNum: false,
  },
  //模式选择
  chapterTitle(e) {
    var that = this
    // console.log(e.currentTarget.dataset.index)
    if (e.currentTarget.dataset.index == 1) {

    }
    that.setData({
      title: e.currentTarget.dataset.index
    })
  },
  //删除错题
  deleteExam(e){
    var that = this
    // console.log(e.currentTarget.dataset.id)
    wx.showModal({
      title: '警告',
      content: "确定将此题从你的错题库删除？",
      success: function (res) {
        if (res.confirm) {
          deleteExams(that, e.currentTarget.dataset.id, newIndex)
        }
      }
    })
  },
  //关闭视频
  videoClose(e) {
    this.setData({
      videoNum: false,
    })
  },
  //打开视频
  videoOpen(e) {
    this.setData({
      videoNum: true,
    })
  },
  /*选择(单选) */
  viewClick: function (e) {
    var that = this
    // console.log(e)
    var select = e.currentTarget.dataset.id
    var indexs = e.currentTarget.dataset.index
    var c_id = e.currentTarget.dataset.cid
    // console.log(c_id)
    
    // 选项没被选择时将执行 
    if (that.data.topicArr[indexs].subject.my_yes == '') {
      // 将选项设置为“已被选择” 
      var arr = that.data.topicArr
      let newObj = arr[indexs]
      newObj.subject.my_yes = select
      arr.splice(indexs, 1, newObj)

      let obj = that.data.topicObj
      obj.subject.my_yes = select
      let topicYes = that.data.topicYes
      let topicNo = that.data.topicNo
      // console.log(c_id, 2, select)
      // let arrs1 = that.data.arrs1
      // let arrs2 = that.data.arrs2
      // let arrs3 = that.data.arrs3
      // let arrs4 = that.data.arrs4
      // let arrs5 = that.data.arrs5
      // if (indexs < 300) {
      //   if (select == newObj.subject.item_true) {
      //     arrs1[indexs].subject.status = 1
      //   } else {
      //     arrs1[indexs].subject.status = 2
      //   }
      //   that.setData({
      //     arrs1: arrs1
      //   })
      // }
      // if (indexs < 600 && indexs > 299) {
      //   if (select == newObj.subject.item_true) {
      //     arrs2[indexs - 300].subject.status = 1
      //   } else {
      //     arrs2[indexs - 300].subject.status = 2
      //   }
      //   that.setData({
      //     arrs2: arrs2
      //   })
      // }
      // if (indexs < 900 && indexs > 599) {
      //   if (select == newObj.subject.item_true) {
      //     arrs3[indexs - 600].subject.status = 1
      //   } else {
      //     arrs3[indexs - 600].subject.status = 2
      //   }
      //   that.setData({
      //     arrs3: arrs3
      //   })
      // }
      // if (indexs < 1200 && indexs > 899) {
      //   if (select == newObj.subject.item_true) {
      //     arrs4[indexs - 900].subject.status = 1
      //   } else {
      //     arrs4[indexs - 900].subject.status = 2
      //   }
      //   that.setData({
      //     arrs4: arrs4
      //   })
      // }
      // if (indexs > 1200) {
      //   if (select == newObj.subject.item_true) {
      //     arrs5[indexs - 1200].subject.status = 1
      //   } else {
      //     arrs5[indexs - 1200].subject.status = 2
      //   }
      //   that.setData({
      //     arrs5: arrs5
      //   })
      // }
      if (select == newObj.subject.item_true) {
        topicYes++
      } else {
        topicNo++
      }
      that.setData({
        topicObj: obj,
        topicArr: arr,
        topicYes: topicYes,
        topicNo: topicNo,
      })
    }
    // console.log(that.data.arrTitle)
  },
  /*选择(多选) */
  viewClick2(e) {
    var that = this
    let topicObj = that.data.topicObj
    // console.log(e.currentTarget.dataset.topic, e.currentTarget.dataset.id)
    if (e.currentTarget.dataset.topic == 'A') {
      if (topicObj.subject.topics[0] == "") {
        topicObj.subject.topics[0] = {
          name: e.currentTarget.dataset.topic,
          content: e.currentTarget.dataset.id
        }
      } else {
        topicObj.subject.subject.topics[0] = ''
      }
    }
    if (e.currentTarget.dataset.topic == 'B') {
      if (topicObj.subject.topics[1] == "") {
        topicObj.subject.topics[1] = {
          name: e.currentTarget.dataset.topic,
          content: e.currentTarget.dataset.id
        }
      } else {
        topicObj.subject.topics[1] = ''
      }
    }
    if (e.currentTarget.dataset.topic == 'C') {
      if (topicObj.subject.topics[2] == "") {
        topicObj.subject.topics[2] = {
          name: e.currentTarget.dataset.topic,
          content: e.currentTarget.dataset.id
        }
      } else {
        topicObj.subject.topics[2] = ''
      }
    }
    if (e.currentTarget.dataset.topic == 'D') {
      if (topicObj.subject.topics[3] == "") {
        topicObj.subject.topics[3] = {
          name: e.currentTarget.dataset.topic,
          content: e.currentTarget.dataset.id
        }
      } else {
        topicObj.subject.topics[3] = ''
      }
    }
    that.setData({
      topicObj: topicObj
    })

  },

  //多选确定
  buttonYes(e) {
    var that = this
    let topicObj = that.data.topicObj
    let words = topicObj.subject.item_true
    let newKeyArr = []
    let result_ture = []
    let c_id = e.currentTarget.dataset.cid
    // console.log(topicObj.subject.topics)
    let nua = 0
    for (let p = 0; p < topicObj.subject.topics.length; p++) {
      if (topicObj.subject.topics[p] != '') {
        nua++
      }
    }
    if (nua < 2) {
      wx.showToast({
        title: "至少选择两项",
        icon: 'none',
        duration: 1000
      })
    } else {
      topicObj.subject.my_yes = topicObj.subject.topics
      // console.log(topicObj)
      var indexs = e.currentTarget.dataset.index
      for (let j = 0; words.indexOf("|") + 1 > 0; j++) {
        let newKey = words.slice(0, words.indexOf("|"))
        words = words.slice(words.indexOf("|") + 1)
        newKeyArr.push(newKey)
        if (words.indexOf("|") < 0) {
          newKeyArr.push(words)
        }
      }
      // 
      for (let g = 0; g < topicObj.subject.option.length; g++) {
        let optionTrue = false
        let optionContent = ''
        for (let f = 0; f < newKeyArr.length; f++) {
          if (topicObj.subject.option[g] == newKeyArr[f]) {
            optionTrue = true
            optionContent = newKeyArr[f]
          }
        }
        if (optionTrue) {
          result_ture.push(optionContent)
        } else {
          result_ture.push('')
        }
      }
      // console.log(result_ture, "result_ture")
      topicObj.subject.result_ture = result_ture
      let num = 0
      // console.log(newKeyArr, topicObj.subject.topics)
      for (let x = 0; x < newKeyArr.length; x++) {
        for (let y = 0; y < topicObj.subject.topics.length; y++) {
          if (newKeyArr[x] == topicObj.subject.topics[y].content) {
            num++
          }
        }
      }
      let topicYes = that.data.topicYes
      let topicNo = that.data.topicNo
      // let arrs1 = that.data.arrs1
      // let arrs2 = that.data.arrs2
      // let arrs3 = that.data.arrs3
      // let arrs4 = that.data.arrs4
      // let arrs5 = that.data.arrs5
      // if (indexs < 300) {
      //   if (num == newKeyArr.length) {
      //     arrs1[indexs].subject.status = 1
      //   } else {
      //     arrs1[indexs].subject.status = 2
      //   }
      //   that.setData({
      //     arrs1: arrs1
      //   })
      // }
      // if (indexs < 600 && indexs > 299) {
      //   if (num == newKeyArr.length) {
      //     arrs2[indexs - 300].subject.status = 1
      //   } else {
      //     arrs2[indexs - 300].subject.status = 2
      //   }
      //   that.setData({
      //     arrs2: arrs2
      //   })
      // }
      // if (indexs < 900 && indexs > 599) {
      //   if (num == newKeyArr.length) {
      //     arrs3[indexs - 600].subject.status = 1
      //   } else {
      //     arrs3[indexs - 600].subject.status = 2
      //   }
      //   that.setData({
      //     arrs3: arrs3
      //   })
      // }
      // if (indexs < 1200 && indexs > 899) {
      //   if (num == newKeyArr.length) {
      //     arrs4[indexs - 900].subject.status = 1
      //   } else {
      //     arrs4[indexs - 900].subject.status = 2
      //   }
      //   that.setData({
      //     arrs4: arrs4
      //   })
      // }
      // if (indexs > 1200) {
      //   if (num == newKeyArr.length) {
      //     arrs5[indexs - 1200].subject.status = 1
      //   } else {
      //     arrs5[indexs - 1200].subject.status = 2
      //   }
      //   that.setData({
      //     arrs5: arrs5
      //   })
      // }
      if (num == newKeyArr.length) {
        topicObj.subject.yesORno = 1
        topicYes++
      } else {
        topicObj.subject.yesORno = 2
        topicNo++
        mistakesArr.push(c_id)
      }
      var arr = that.data.topicArr
      arr.splice(indexs, 1, topicObj)
      // console.log(topicObj)
      that.setData({
        topicObj: topicObj,
        topicYes: topicYes,
        topicNo: topicNo,
        topicArr: arr,

      })
    }

  },

  //上一页
  previous(e) {
    var that = this
    let arrs = that.data.topicArr
    if (newIndex > 0) {
      // console.log("上一页")
      newIndex = newIndex - 1
      time2 = 1;
      if (arrs[newIndex].subject.culture == 1 && !arrs[newIndex].subject.topics) {
        arrs[newIndex].subject.topics = ['', '', '', '']
      }
      let topicObj = arrs[newIndex]
      if (!topicObj.subject.my_yes) {
        topicObj.subject.my_yes = ''
      }
      let optionArr = []
      if (topicObj.subject.item1 != '') {
        optionArr.push(topicObj.subject.item1)
      }
      if (topicObj.subject.item2 != '') {
        optionArr.push(topicObj.subject.item2)
      }
      if (topicObj.subject.item3 != '') {
        optionArr.push(topicObj.subject.item3)
      }
      if (topicObj.subject.item4 != '') {
        optionArr.push(topicObj.subject.item4)
      }
      topicObj.subject.option = optionArr
      let topicObjContent = topicObj.subject.content//获取题目
      let topicObjKey = topicObj.subject.key_words//获取关键字
      let falseKey = topicObj.subject.key_false//获取关键字
      let tureKey = topicObj.subject.key_true//获取关键字
      if (topicObjKey) {
        keyWords(that, topicObjContent, topicObjKey)
      } else {
        that.setData({
          arrTitle: [],
        })
      }

      if (falseKey != '') {
        falseKeys(that, falseKey, topicObj.subject.option)
      } else {
        that.setData({
          falseOption: ['', '', '', '']
        })
      }
      if (tureKey != '') {
        tureKeys(that, tureKey, topicObj.subject.option)
      } else {
        that.setData({
          tureOption: ['', '', '', '']

        })
      }
      // if (arrs[newIndex].culture == 1) {
      //   arrs[newIndex].subject.topics = ['', '', '', '']
      // }

      let words = topicObj.subject.item_true
      let newKeyArr = []
      let result_ture = []
      for (let j = 0; words.indexOf("|") + 1 > 0; j++) {
        let newKey = words.slice(0, words.indexOf("|"))
        words = words.slice(words.indexOf("|") + 1)
        newKeyArr.push(newKey)
        if (words.indexOf("|") < 0) {
          newKeyArr.push(words)
        }
      }
      // 
      for (let g = 0; g < topicObj.subject.option.length; g++) {
        let optionTrue = false
        let optionContent = ''
        for (let f = 0; f < newKeyArr.length; f++) {
          if (topicObj.subject.option[g] == newKeyArr[f]) {
            optionTrue = true
            optionContent = newKeyArr[f]
          }
        }
        if (optionTrue) {
          result_ture.push(optionContent)
        } else {
          result_ture.push('')
        }
      }
      topicObj.subject.result_ture = result_ture
      // console.log(arrs[newIndex])
      // console.log(topicObj)

      that.setData({
        topicArr: arrs,
        // topicObj: arrs[newIndex],
        topicObj: topicObj,
        view1: 'selection1',
        view2: 'selection1',
        view3: 'selection1',
        view4: 'selection1',
        topicArrInd: newIndex,
      })
    } else {
      wx.showToast({
        title: "这已经是第一题了",
        icon: 'none',
        duration: 1000
      })
    }
  },
  //下一页
  next(e) {
    var that = this
    let arrs = that.data.topicArr

    if (newIndex < arrs.length - 1) {
      // console.log("下一页")
      time2 = 2;
      newIndex = newIndex + 1
      if (arrs[newIndex].subject.culture == 1 && !arrs[newIndex].subject.topics) {
        arrs[newIndex].subject.topics = ['', '', '', '']
      }
      let topicObj = arrs[newIndex]
      if (!topicObj.subject.my_yes) {
        topicObj.subject.my_yes = ''
      }
      let optionArr = []
      if (topicObj.subject.item1 != '') {
        optionArr.push(topicObj.subject.item1)
      }
      if (topicObj.subject.item2 != '') {
        optionArr.push(topicObj.subject.item2)
      }
      if (topicObj.subject.item3 != '') {
        optionArr.push(topicObj.subject.item3)
      }
      if (topicObj.subject.item4 != '') {
        optionArr.push(topicObj.subject.item4)
      }
      topicObj.subject.option = optionArr
      let topicObjContent = topicObj.subject.content//获取题目
      let topicObjKey = topicObj.subject.key_words//获取关键字
      let falseKey = topicObj.subject.key_false//获取关键字
      let tureKey = topicObj.subject.key_true//获取关键字
      if (topicObjKey) {
        keyWords(that, topicObjContent, topicObjKey)
      } else {
        that.setData({
          arrTitle: [],
        })
      }
      if (falseKey != '') {
        falseKeys(that, falseKey, topicObj.subject.option)
      } else {
        that.setData({
          falseOption: ['', '', '', '']
        })
      }
      if (tureKey != '') {
        tureKeys(that, tureKey, topicObj.subject.option)
      } else {
        that.setData({
          tureOption: ['', '', '', '']
        })
      }
      // if (arrs[newIndex].culture == 1) {
      //   arrs[newIndex].topics = ['', '', '', '']
      // }
      let words = topicObj.subject.item_true
      let newKeyArr = []
      let result_ture = []
      for (let j = 0; words.indexOf("|") + 1 > 0; j++) {
        let newKey = words.slice(0, words.indexOf("|"))
        words = words.slice(words.indexOf("|") + 1)
        newKeyArr.push(newKey)
        if (words.indexOf("|") < 0) {
          newKeyArr.push(words)
        }
      }
      // 
      for (let g = 0; g < topicObj.subject.option.length; g++) {
        let optionTrue = false
        let optionContent = ''
        for (let f = 0; f < newKeyArr.length; f++) {
          if (topicObj.subject.option[g] == newKeyArr[f]) {
            optionTrue = true
            optionContent = newKeyArr[f]
          }
        }
        if (optionTrue) {
          result_ture.push(optionContent)
        } else {
          result_ture.push('')
        }
      }
      topicObj.subject.result_ture = result_ture
      // console.log(arrs[newIndex])
      // console.log(topicObj)
      that.setData({
        topicArr: arrs,
        // topicObj: arrs[newIndex],
        topicObj: topicObj,
        view1: 'selection1',
        view2: 'selection1',
        view3: 'selection1',
        view4: 'selection1',
        topicArrInd: newIndex,

      })
    } else {
      wx.showToast({
        title: "这已经是最后一题了",
        icon: 'none',
        duration: 1000
      })
    }

  },


  //题号查看
  chapterClickTop(e) {
    var that = this
    var chapterClickTops = that.data.chapterClickTops
    let widthBL = 750 / wx.getSystemInfoSync().windowWidth
    let heightWin = wx.getSystemInfoSync().windowHeight / 2 - (80 / widthBL)
    var animation2 = wx.createAnimation({});
    if (chapterClickTops) {
      wx.showToast({
        title: "正在加载",
        icon: "loading",
        mask: "ture",
        duration: 1200
      })
      animation2.translate(0, -heightWin).step({ duration: 500 })
      that.setData({
        chapterClickTops: false,
        animationData: animation2.export()
      })
    } else {
      animation2.translate(0, 0).step({ duration: 500 })
      that.setData({
        chapterClickTops: true,
        animationData: animation2.export()
      })
    }

  },

  //题目跳转
  topicClick(e) {
    var that = this
    // console.log(e.currentTarget.dataset.index)
    newIndex = e.currentTarget.dataset.index
    let arrs = that.data.topicArr

    if (arrs[newIndex].subject.culture == 1 && !arrs[newIndex].subject.topics) {
      arrs[newIndex].subject.topics = ['', '', '', '']
    }
    var animation2 = wx.createAnimation({});
    animation2.translate(0, 0).step({ duration: 500 })
    let topicObj = arrs[newIndex]
    if (!topicObj.subject.my_yes) {
      topicObj.subject.my_yes = ''
    }
    let optionArr = []
    if (topicObj.subject.item1 != '') {
      optionArr.push(topicObj.subject.item1)
    }
    if (topicObj.subject.item2 != '') {
      optionArr.push(topicObj.subject.item2)
    }
    if (topicObj.subject.item3 != '') {
      optionArr.push(topicObj.subject.item3)
    }
    if (topicObj.subject.item4 != '') {
      optionArr.push(topicObj.subject.item4)
    }
    topicObj.subject.option = optionArr
    let topicObjContent = topicObj.subject.content//获取题目
    let topicObjKey = topicObj.subject.key_words//获取关键字
    let falseKey = topicObj.subject.key_false//获取关键字
    let tureKey = topicObj.subject.key_true//获取关键字
    if (topicObjKey) {
      keyWords(that, topicObjContent, topicObjKey)
    } else {
      that.setData({
        arrTitle: [],
      })
    }

    if (falseKey != '') {
      falseKeys(that, falseKey, topicObj.subject.option)
    } else {
      that.setData({
        falseOption: ['', '', '', '']
      })
    }
    if (tureKey != '') {
      tureKeys(that, tureKey, topicObj.subject.option)
    } else {
      that.setData({
        tureOption: ['', '', '', '']
      })
    }
    // if (arrs[e.currentTarget.dataset.index].culture == 1) {
    //   arrs[e.currentTarget.dataset.index].topics = ['', '', '', '']
    // }
    // console.log(topicObj)
    let words = topicObj.subject.item_true
    let newKeyArr = []
    let result_ture = []
    for (let j = 0; words.indexOf("|") + 1 > 0; j++) {
      let newKey = words.slice(0, words.indexOf("|"))
      words = words.slice(words.indexOf("|") + 1)
      newKeyArr.push(newKey)
      if (words.indexOf("|") < 0) {
        newKeyArr.push(words)
      }
    }
    // 
    for (let g = 0; g < topicObj.subject.option.length; g++) {
      let optionTrue = false
      let optionContent = ''
      for (let f = 0; f < newKeyArr.length; f++) {
        if (topicObj.subject.option[g] == newKeyArr[f]) {
          optionTrue = true
          optionContent = newKeyArr[f]
        }
      }
      if (optionTrue) {
        result_ture.push(optionContent)
      } else {
        result_ture.push('')
      }
    }
    topicObj.subject.result_ture = result_ture
    that.setData({
      topicArr: arrs,
      // topicObj: arrs[e.currentTarget.dataset.index],
      topicObj: topicObj,
      topicArrInd: e.currentTarget.dataset.index,
      chapterClickTops: true,
      animationData: animation2.export()
    })
  },


  //获取语音
  audioClick(e) {
    // console.log(e.currentTarget.dataset.src)
    const innerAudioContext = wx.createInnerAudioContext()
    innerAudioContext.autoplay = true
    innerAudioContext.src = e.currentTarget.dataset.src
    innerAudioContext.onPlay(() => {
      // console.log('开始播放')
    })
    innerAudioContext.onError((res) => {
      // console.log(res.errMsg)
      // console.log(res.errCode)
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {


    // console.log(app.chapterStudy)
    // console.log(options)
    var that = this
    // let chapterName = ''
    // if (options.type == 1){
      // chapterName = "难题解析"
      errorExam(that)
    // }
    // if (options.type == 2) {
    //   chapterName = "我的错题"
    //   errorPractice(that)
    // }
   

    let widthBL = 750 / wx.getSystemInfoSync().windowWidth


    newIndex = 0
    that.setData({
      heights: wx.getSystemInfoSync().windowHeight / 2,
      heightx: 80 / widthBL,
      heightd: wx.getSystemInfoSync().windowHeight / 2 - (80 / widthBL),
      // chapterName: chapterName,
      title: 1,
    })
    // console.log(that.data.objNew)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.hideShareMenu()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },


  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})




//关键字
function keyWords(that, texts, wordss) {
  let words = wordss;
  let contents = texts;
  let arrTitle = []
  // console.log(words)
  // console.log(words, words.indexOf(","))
  let newKeyArr = []
  if (words.indexOf(",") > 0) {
    for (let j = 0; words.indexOf(",") + 1 > 0; j++) {
      let newKey = words.slice(0, words.indexOf(","))
      words = words.slice(words.indexOf(",") + 1)
      newKeyArr.push(newKey)
      if (words.indexOf(",") < 0) {
        newKeyArr.push(words)
      }
    }
  } else {
    newKeyArr.push(words)
  }

  // console.log(newKeyArr)
  for (let i = 0; i < newKeyArr.length; i++) {
    let obj1 = {
      content: contents.substring(0, contents.indexOf(newKeyArr[i])),
      font: 1
    }
    arrTitle.push(obj1)
    let obj2 = {
      content: newKeyArr[i],
      font: 2
    }
    arrTitle.push(obj2)
    if (i + 1 == newKeyArr.length) {
      let obj3 = {
        content: contents.substring(contents.indexOf(newKeyArr[i]) + newKeyArr[i].length),
        font: 1
      }
      arrTitle.push(obj3)
    }
    contents = contents.substring(contents.indexOf(newKeyArr[i]) + newKeyArr[i].length)
  }
  that.setData({
    arrTitle: arrTitle,
  })
}

//错误提示
function falseKeys(that, wordss, arrOption) {
  // console.log(wordss, arrOption)
  let words = wordss;
  let falseOption = ['', '', '', '']
  let newKeyArr = []
  if (words.indexOf(",") > 0) {
    for (let j = 0; words.indexOf(",") + 1 > 0; j++) {
      let newKey = words.slice(0, words.indexOf(","))
      words = words.slice(words.indexOf(",") + 1)
      newKeyArr.push(newKey)
      if (words.indexOf(",") < 0) {
        newKeyArr.push(words)
      }
    }
  } else {
    newKeyArr.push(words)
  }
  for (let k = 0; k < arrOption.length; k++) {
    let newArrs = []
    let contents = arrOption[k]
    for (let i = 0; i < newKeyArr.length; i++) {
      if (contents.indexOf(newKeyArr[i]) < 0) {
      } else {
        let obj1 = {
          content: contents.substring(0, contents.indexOf(newKeyArr[i])),
          font: 1
        }
        newArrs.push(obj1)
        let obj2 = {
          content: newKeyArr[i],
          font: 2
        }
        newArrs.push(obj2)
        contents = contents.substring(contents.indexOf(newKeyArr[i]) + newKeyArr[i].length)

      }
      if (i + 1 == newKeyArr.length) {
        let obj3 = {
          content: contents,
          font: 1
        }
        newArrs.push(obj3)
      }
    }
    falseOption[k] = newArrs
  }

  that.setData({
    falseOption: falseOption

  })
}
//正确提示
function tureKeys(that, wordss, arrOption) {
  // console.log(wordss, arrOption)
  let words = wordss;
  let tureOption = ['', '', '', '']
  let newKeyArr = []
  if (words.indexOf(",") > 0) {
    for (let j = 0; words.indexOf(",") + 1 > 0; j++) {
      let newKey = words.slice(0, words.indexOf(","))
      words = words.slice(words.indexOf(",") + 1)
      newKeyArr.push(newKey)
      if (words.indexOf(",") < 0) {
        newKeyArr.push(words)
      }
    }
  } else {
    newKeyArr.push(words)
  }
  for (let k = 0; k < arrOption.length; k++) {
    let newArrs = []
    let contents = arrOption[k]
    for (let i = 0; i < newKeyArr.length; i++) {
      if (contents.indexOf(newKeyArr[i]) < 0) {
      } else {
        let obj1 = {
          content: contents.substring(0, contents.indexOf(newKeyArr[i])),
          font: 1
        }
        newArrs.push(obj1)
        let obj2 = {
          content: newKeyArr[i],
          font: 2
        }
        newArrs.push(obj2)
        contents = contents.substring(contents.indexOf(newKeyArr[i]) + newKeyArr[i].length)
      }
      if (i + 1 == newKeyArr.length) {
        let obj3 = {
          content: contents,
          font: 1
        }
        newArrs.push(obj3)
      }
    }
    tureOption[k] = newArrs

  }
  // console.log(tureOption, "tureOption")
  that.setData({
    tureOption: tureOption
  })
}

//删除错题
function deleteExams(that, id, index) {
    wx.request({
      url: 'https://minitest.17jxyx.com/api/v1/analog/error/' + id,
      method: "DELETE",
      header: {
        token: app.token
      },
      success: function (res) {
        // console.log(res)
        let arr = that.data.topicArr
        arr.splice(index, 1)
        that.setData({
          topicArr: arr,
          topicObj: arr[index]
        })
        // wx.showToast({
        //   title: "至少选择两项",
        //   icon: 'none',
        //   duration: 1000
        // })
        // console.log(res.data[3])

      },
      fail: function (res) {
        console.log(res)
      }
    })

}
//考试错题
function errorExam(that) {
  // console.log(course, car_status, "gdashjgdshja")
  wx.request({
    url: 'https://minitest.17jxyx.com/api/v1/test/error/' + app.titleStudy + '/' + app.cardType,
    method: "GET",
    header: {
      token: app.token
    },

    success: function (res) {
      // console.log(res)
      if(res.data.length > 0){
        dispose(that, res)
      }
    },
    fail: function (res) {
      // console.log(res)
    }
  })
}

//题目处理
function dispose(that,res){
  let ind = 0
  let arrNew = res.data
  if (arrNew[ind].subject.culture == 1 && !arrNew[ind].subject.topics) {
    arrNew[ind].subject.topics = ['', '', '', '']
  }
  let topicObj = arrNew[ind]
  topicObj.subject.my_yes = ''
  let optionArr = []
  if (topicObj.subject.item1 != '') {
    optionArr.push(topicObj.subject.item1)
  }
  if (topicObj.subject.item2 != '') {
    optionArr.push(topicObj.subject.item2)
  }
  if (topicObj.subject.item3 != '') {
    optionArr.push(topicObj.subject.item3)
  }
  if (topicObj.subject.item4 != '') {
    optionArr.push(topicObj.subject.item4)
  }
  topicObj.subject.option = optionArr
  let topicObjContent = topicObj.subject.content//获取题目
  let topicObjKey = topicObj.subject.key_words//获取关键字
  let falseKey = topicObj.subject.key_false//获取关键字
  let tureKey = topicObj.subject.key_true//获取关键字

  if (topicObjKey) {
    keyWords(that, topicObjContent, topicObjKey)
  } else {
    that.setData({
      arrTitle: [],
    })
  }
  if (falseKey != '') {
    falseKeys(that, falseKey,topicObj.subject.option)
  } else {
    that.setData({
      falseTitle: [],
    })
  }
  if (tureKey != '') {
    tureKeys(that, tureKey,topicObj.subject.option)
  } else {
    that.setData({
      tureTitle: [],
    })
  }
  let arrs = res.data
  if (arrs[0].culture == 1) {
    arrs[0].subject.topics = ['', '', '', '']
  }
  // console.log(topicObj)
  let words = topicObj.subject.item_true
  let newKeyArr = []
  let result_ture = []
  for (let j = 0; words.indexOf("|") + 1 > 0; j++) {
    let newKey = words.slice(0, words.indexOf("|"))
    words = words.slice(words.indexOf("|") + 1)
    newKeyArr.push(newKey)
    if (words.indexOf("|") < 0) {
      newKeyArr.push(words)
    }
  }
  // 
  for (let g = 0; g < topicObj.subject.option.length; g++) {
    let optionTrue = false
    let optionContent = ''
    for (let f = 0; f < newKeyArr.length; f++) {
      if (topicObj.subject.option[g] == newKeyArr[f]) {
        optionTrue = true
        optionContent = newKeyArr[f]
      }
    }
    if (optionTrue) {
      result_ture.push(optionContent)
    } else {
      result_ture.push('')
    }
  }
  topicObj.subject.result_ture = result_ture
  that.setData({
    topicYes: 0,
    topicNo: 0,
    topicArr: arrs,
    topicObj: topicObj,
    gif: "false",
    topicArrInd: 0

  })
  // let arrs0 = arrs
  // let arrs1 = []
  // let arrs2 = []
  // let arrs3 = []
  // let arrs4 = []
  // let arrs5 = []

  // arrs1 = arrs0.slice(0, 300)
  // arrs2 = arrs0.slice(300, 600)
  // arrs3 = arrs0.slice(600, 900)
  // arrs4 = arrs0.slice(900, 1200)
  // arrs5 = arrs0.slice(1200)
  // console.log(arrs0.slice(2200))
  // that.setData({
  //   arrs1: arrs1,
  //   arrs2: arrs2,
  //   arrs3: arrs3,
  //   arrs4: arrs4,
  //   arrs5: arrs5,
  // })
}
